package com.xerland.tbootstrap.ui;

import android.content.Context;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.Toast;

import com.xerland.tbootstrap.R;
import com.xerland.tbootstrap.utils.SpfManager;


/**
 * Fileame: com.xerland.tbootstrap.ui
 * Desription:
 * Class and methods:
 * <p>
 * Author: Anson Chan
 * Copyright: 2016 Xerland Co. Ltd. All rights reserved.
 * Contact:    anson_chan@xerland.com
 * Deffield    updated: Updated
 */
public class DrawerMenuListener implements View.OnClickListener {

    public static final int VALIDATE_CUR_PW_EMPTY = 1;
    public static final int VALIDATE_CUR_PW_ERROR = 2;
    public static final int VALIDATE_NEW_PW_SHORT = 3;
    public static final int VALIDATE_CONFIRM_PW_EMPTY = 4;
    public static final int VALIDATE_CONFIRM_PW_ERROR = 5;
    public static final int VALIDATE_SVR_PORT_ERROR = 6;
    private View.OnClickListener comitListener;
    private Context context;
    private int layoutRsId;
    private SpfManager spfManager;
    private AlertDialog alertDialog;
    private Window window;


    public DrawerMenuListener(Context context, int layoutRsId) {
        this.context = context;
        this.layoutRsId = layoutRsId;
        spfManager = new SpfManager(context);
        alertDialog = new AlertDialog.Builder(context).create();
        window = alertDialog.getWindow();
    }

    public void setCommitListener(View.OnClickListener comitListener) {
        this.comitListener = comitListener;
    }

    @Override
    public void onClick(View v) {
        alertDialog.show();
        window.setContentView(layoutRsId);
        onLoad();
        window.findViewById(android.R.id.button1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onCommit();
            }
        });
        window.findViewById(android.R.id.button2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
            }
        });
        window.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
    }

    private void onLoad() {
        switch (layoutRsId){
            case R.layout.dialog_connection:
                loadConnection();
                break;
            case R.layout.dialog_account:
                loadAccount();
                break;
            default:
                break;
        }
    }

    private void loadConnection() {
        EditText serverUrlEt = (EditText)window.findViewById(R.id.et_connection_server);
        EditText serverPortEt = (EditText)window.findViewById(R.id.et_connection_port);
        EditText baseTopic = (EditText)window.findViewById(R.id.et_connection_subject);
        EditText peerIdEt = (EditText)window.findViewById(R.id.et_connection_peer);
        serverUrlEt.setText(spfManager.loadServerUrl());
        serverPortEt.setText(Integer.toString(spfManager.loadServerPort()));
        baseTopic.setText(spfManager.loadBaseTopic());
        peerIdEt.setText(spfManager.loadPeerId());
    }

    private void loadAccount() {
        EditText currentUserIdEt = (EditText)window.findViewById(R.id.et_user_current);
        currentUserIdEt.setText(spfManager.loadUserId());
    }

    private void onCommit() {
        switch (layoutRsId) {
            case R.layout.dialog_connection:
                handleConnectionCommit();
                break;
            case R.layout.dialog_account:
                handleAccountCommit();
                break;
            default:
                break;
        }
    }

    public void handleConnectionCommit() {
        EditText serverUrlEt = (EditText)window.findViewById(R.id.et_connection_server);
        EditText serverPortEt = (EditText)window.findViewById(R.id.et_connection_port);
        EditText baseTopic = (EditText)window.findViewById(R.id.et_connection_subject);
        EditText peerIdEt = (EditText)window.findViewById(R.id.et_connection_peer);
//        if( !Pattern.compile("[0-9]*").matcher(serverPortEt.getText().toString()).matches())
        if(!TextUtils.isDigitsOnly(serverPortEt.getText().toString()))
        {
            validateToast(VALIDATE_SVR_PORT_ERROR);
            return;
        }
        if( serverUrlEt.length() != 0) {
            spfManager.saveServerUrl(serverUrlEt.getText().toString());
        }
        if(serverPortEt.length() != 0) {
            spfManager.saveServerPort(Integer.valueOf(serverPortEt.getText().toString()).intValue());
        }
        if (baseTopic.length() != 0) {
            spfManager.saveBaseTopic(baseTopic.getText().toString());
        }
        if (baseTopic.length() != 0) {
            spfManager.savePeerId(peerIdEt.getText().toString());
        }
        alertDialog.dismiss();
    }

    public void handleAccountCommit() {
        EditText currentUserIdEt = (EditText)window.findViewById(R.id.et_user_current);
        EditText newUserIdEt = (EditText)window.findViewById(R.id.et_user_new);
        EditText currentPasswordEt = (EditText)window.findViewById(R.id.et_password_current);
        EditText newPasswordEt = (EditText)window.findViewById(R.id.et_password_new);
        EditText confirmPasswordEt = (EditText)window.findViewById(R.id.et_password_confirm);

        if (currentPasswordEt.length() == 0) {
            validateToast(VALIDATE_CUR_PW_EMPTY);
            return;
        }
        if (!currentPasswordEt.getText().toString().equals(spfManager.loadUserCipher())) {
            validateToast(VALIDATE_CUR_PW_ERROR);
            return;
        }
        if (newPasswordEt.length() != 0) {
            if (newPasswordEt.length() < 6) {
                validateToast(VALIDATE_NEW_PW_SHORT);
                return;
            }
            if (confirmPasswordEt.length() == 0) {
                validateToast(VALIDATE_CONFIRM_PW_EMPTY);
                return;
            }
            if (!confirmPasswordEt.getText().toString().equals(newPasswordEt.getText().toString())) {
                validateToast(VALIDATE_CONFIRM_PW_ERROR);
                return;
            }
        }

        if( newUserIdEt.length() != 0) {
            spfManager.saveUserId(newUserIdEt.getText().toString());
        }
        if(newPasswordEt.length() != 0) {
            spfManager.saveUserCipher(newPasswordEt.getText().toString());
        }
        alertDialog.dismiss();
    }

    private void validateToast(int code) {
        switch (code) {
            case VALIDATE_CUR_PW_EMPTY:
                Toast.makeText(context, context.getString(R.string.validate_cur_pw_empty), Toast.LENGTH_SHORT).show();
                break;
            case VALIDATE_CUR_PW_ERROR:
                Toast.makeText(context, context.getString(R.string.validate_cur_pw_error), Toast.LENGTH_SHORT).show();
                break;
            case VALIDATE_NEW_PW_SHORT:
                Toast.makeText(context, context.getString(R.string.validate_new_pw_short), Toast.LENGTH_SHORT).show();
                break;
            case VALIDATE_CONFIRM_PW_EMPTY:
                Toast.makeText(context, context.getString(R.string.validate_confirm_pw_empty), Toast.LENGTH_SHORT).show();
                break;
            case VALIDATE_CONFIRM_PW_ERROR:
                Toast.makeText(context, context.getString(R.string.validate_confirm_pw_error), Toast.LENGTH_SHORT).show();
                break;
            case VALIDATE_SVR_PORT_ERROR:
                Toast.makeText(context, context.getString(R.string.validate_svr_port_error), Toast.LENGTH_SHORT).show();
                break;
            default:
                break;
        }
    }
}
